﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlBuilder
{
    public class AutoSerialize<T> where T : new() 
    {
        public AutoSerialize(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("AutoSerialize.Name");
            SerializeFileName = name;
            var value = Functions.DeserializeObjForFile<T>(name);
            if (value == null)
            {
                value = new T();
                Functions.SerializeObjToFile(value!, name);
            }
            Value = value;
        }

        public string SerializeFileName { get; } = string.Empty;
        public T? Value { get; set; }
       
        public void Save()
        {
            Functions.SerializeObjToFile(this.Value!, this.SerializeFileName);
        }
    }
}
